﻿; var isMobile = (function (userAgent) {
    return !!userAgent.match(/android|webos|ip(hone|ad|od)|opera (mini|mobi|tablet)|iemobile|windows.+(phone|touch)|mobile|fennec|kindle (Fire)|Silk|maemo|blackberry|playbook|bb10\; (touch|kbd)|Symbian(OS)|Ubuntu Touch/i);
}(navigator.userAgent));
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

$.fn.serializeJson = function () {
    return JSON.stringify(this.serializeObject())   
};

var object2query = function (param, key, encode) {
    if (param == null) return '';
    var paramStr = '';
    var t = typeof(param);
    if (t == 'string' || t == 'number' || t == 'boolean') {
        paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
    } else {
        for (var i in param) {
            var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
            paramStr += object2query(param[i], k, encode);
        }
    }
    return paramStr;
}; 

function query2object(s) {
    return JSON.parse('{"' + decodeURI(s.replace(/&/g, "\",\"").replace(/=/g, "\":\"")) + '"}')
}

function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return decodeURIComponent(r[2]);
    };
    return null;
}


/*
* changeURLStatic 修改地址栏URL参数 不跳转
*   @param name 参数名
*   @param value 参数值
*
* */
function changeURLState(name, value) {
    var url = location.href;
    var reg = eval('/([\?|&]' + name + '=)[^&]*/gi');
    value = value.toString().replace(/(^\s*)|(\s*$)/g, "");  //移除首尾空格
    if (!value) {
        var url2 = url.replace(reg, '');  //正则替换
    } else {
        if (url.match(reg)) {
            var url2 = url.replace(reg, '$1' + value);  //正则替换
        } else {
            var url2 = url + (url.indexOf('?') > -1 ? '&' : '?') + name + '=' + value;  //没有参数添加参数
        }
    }
    history.replaceState(null, null, url2);  //替换地址栏
    // window.history.pushState(state, title, url)
}

var datatable_language = {
    "processing": "加载中...",
    "lengthMenu": "每页显示 _MENU_ 条数据",
    "zeroRecords": "没有匹配结果",
    "info": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
    "infoEmpty": "显示第 0 至 0 项结果，共 0 项",
    "infoFiltered": "(由 _MAX_ 项结果过滤)",
    "infoPostFix": "",
    "search": "搜索:",
    "url": "",
    "emptyTable": "没有匹配结果",
    "loadingRecords": "载入中...",
    "paginate": {
        "first": "首页",
        "previous": "上一页",
        "next": "下一页",
        "last": "末页"
    },
    'select': {
        "rows": {
            _: ' %d 行选中',
            //0: 'Click a row to select',
            //1: '1 row selected'
        }
    },
    "aria": {
        "sortAscending": ": 以升序排列此列",
        "sortDescending": ": 以降序排列此列"
    }
}


var IDreg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{7}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/;

var getIDInfo = function (cardnumber) {
    if (IDreg.test(cardnumber)) {
        
    } else {
        return false
    }
    var info = {
        birthday: "",
        sexy: 0,
    }
    // sex 0未知  1男  2女
    if (cardnumber.length === 15) {
        info.birthday = `19${cardnumber.substr(6, 2)}-${cardnumber.substr(8, 2)}-${cardnumber.substr(10, 2)}`
        info.sexy = (cardnumber.substr(14, 1) % 2) || 2
    } else if (cardnumber.length === 18) {
        info.birthday = `${cardnumber.substr(6, 4)}-${cardnumber.substr(10, 2)}-${cardnumber.substr(12, 2)}`
        info.sexy = (cardnumber.substr(16, 1) % 2) || 2
    } else {
        return false
    }
    return info
}

// gritter-light gritter-error gritter-danger gritter-info gritter-warning gritter-success
var show_alert = function (type, message) {
    // class_name: 'gritter-light'
    var pams = { text: message, time: 600000, }
    if (type == "warning") {
        pams.title = "错误!"
        pams.class_name = "gritter-warning"
    } else if (type == "error") {
        pams.title = "异常!"
        pams.class_name = "gritter-error"
    } else if (type == "success") {
        pams.title = "成功!"
        pams.class_name = "gritter-success"
    } else if (type == "info") {
        pams.title = "提醒!"
        pams.class_name = "gritter-info"
    }
    
    $.gritter.add(pams);
}

var show_notice = function (id) {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/Notice/Detail/" + id,
        success: function (result) {
            if (result.Success) {
                $("#notice-row-" + id).remove()
                show_notice_success(result)
            } else {
                show_alert('warning', result.Message)
            }
        },
        error: function (e) {
            show_alert('error', '网络异常.')
        }
    });

}
var noticeDataTable = null
var show_notice_success = function (result) {
    $("#modal-notice-note").empty()
    $("#modal-notice-note").html(result.Data.Html)

    var count = result.Data.Count //Number($("#notice-total").html())
    //if (count > 0) {
    //    count = count - 1
    //}
    if (count > 0) {
        $("#notice-total").addClass("label label-danger")
        $("#notice-total").html(count)
    }
    else {
        $("#notice-total").removeClass("label label-danger")
        $("#notice-total").html("")
    }
    $("#modal-notice").modal()
    if (noticeDataTable) {
        noticeDataTable.ajax.reload();
    }
}

var show_ajax = function () {
    $("#ajax-wait-modal-dialog").modal()
}

var hide_ajax = function () {
    setTimeout(function () {
        $("#ajax-wait-modal-dialog").modal('hide')
    },500);
}

$(document).ready(function () {
    $(".automatic-card-id").change(function () {
        var info = getIDInfo($(this).val())
        var name = $(this).attr("name")
        if (info) {
            var el_sexy = $(".automatic-sexy-for-" + name)
            var el_birthday = $(".automatic-birthday-for-" + name)
            el_sexy.val(info.sexy);
            el_birthday.val(info.birthday);

            if (el_birthday.datepicker) {
                //el_birthday.datepicker('getDate', info.birthday)
                el_birthday.datepicker('setDate', new Date(info.birthday))
                
            }
            if (el_sexy.selectpicker) {
                el_sexy.selectpicker('refresh');
            }
            
        }
    })
})

